package com.chestnut.contentcore.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chestnut.common.log.annotation.Log;
import com.chestnut.common.log.enums.BusinessType;
import com.chestnut.common.security.web.PageRequest;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.contentcore.domain.CmsCarousel;
import com.chestnut.contentcore.domain.CmsCertificationsStandards;

import com.chestnut.contentcore.service.ICmsCarouselService;
import com.chestnut.system.security.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.chestnut.common.security.web.BaseRestController;

import com.chestnut.common.security.anno.Priv;
import com.chestnut.system.security.AdminUserType;
import lombok.RequiredArgsConstructor;
import com.chestnut.common.domain.R;

/**
 * 轮播图Controller
 *
 * @author chestnut
 * @date 2025-04-28
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/carousel")
public class CmsCarouselController extends BaseRestController {

    @Autowired
    private ICmsCarouselService cmsCarouselService;
    /**
     * 查询轮播图列表
     */

    @GetMapping("/list")
    public R<?> list(CmsCarousel cmsCarousel)
    {
        PageRequest pr = this.getPageRequest();
        LambdaQueryChainWrapper<CmsCarousel> wrapper = this.cmsCarouselService.lambdaQuery();
                    if (cmsCarousel.getName() != null) {
                        wrapper.like(CmsCarousel::getName,cmsCarousel.getName());
                    }
        if (cmsCarousel.getLanguage() != null) {
            wrapper.like(CmsCarousel::getLanguage,cmsCarousel.getLanguage());
        }
        wrapper.orderByAsc(CmsCarousel::getSort);
        Page<CmsCarousel> page = wrapper
                .page(new Page<>(pr.getPageNumber(), pr.getPageSize(), true));

        return this.bindDataTable(page);
    }




    /**
     * 获取轮播图详细信息
     */

    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(this.cmsCarouselService.getById(id));
    }

    /**
     * 新增轮播图
     */

    @PostMapping
    public R<?> add(@RequestBody CmsCarousel cmsCarousel)
    {
        cmsCarousel.setCreateBy(StpAdminUtil.getLoginUser().getUsername());
        cmsCarousel.setCreateTime(LocalDateTime.now());
        this.cmsCarouselService.save(cmsCarousel);
        return R.ok();
    }

    /**
     * 修改轮播图
     */

    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody CmsCarousel cmsCarousel)
    {
        this.cmsCarouselService.updateById(cmsCarousel);
        return R.ok();
    }

    /**
     * 删除轮播图
     */

    @Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        this.cmsCarouselService.removeBatchByIds(List.of(ids));
        return R.ok();
    }
}
